package com.zw.yzk.kabu.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.ShippingAddressModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/10.
 */

public class ShippingAddressAdapter extends RecyclerView.Adapter<ShippingAddressAdapter.Holder> {

    private List<ShippingAddressModel> addressList;
    private OnItemTouchListener listener;
    private boolean addressClickble;

    public ShippingAddressAdapter(List<ShippingAddressModel> list, boolean addressClickble) {
        addressList = new ArrayList<>();
        if (list != null) {
            addressList.addAll(list);
        }
        this.addressClickble = addressClickble;
    }

    public void  refresh(List<ShippingAddressModel> list) {
        addressList.clear();
        if (list != null) {
            addressList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_shipping_address, parent, false);

        return new Holder(rootView);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        ShippingAddressModel model = addressList.get(position);
        holder.name.setText(model.getName());
        holder.address.setText(model.getAddress());
        holder.phone.setText(model.getPhone());
        holder.selectDefault.setChecked(model.isSelected());
        holder.addressLayout.setEnabled(addressClickble);

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int position = holder.getAdapterPosition();
                    listener.onDelete(position, addressList.get(position));
                }
            }
        });

        holder.selectDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                for (ShippingAddressModel m : addressList) {
                    m.setSelected(false);
                }
                addressList.get(position).setSelected(!addressList.get(position).isSelected());
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onSelected(position, addressList.get(position));
                }
            }
        });

        holder.addressLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    int position = holder.getAdapterPosition();
                    listener.onAddressSelected(position, addressList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.phone)
        TextView phone;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.select_default_address)
        RadioButton selectDefault;
        @Bind(R.id.delete_address)
        ImageView delete;
        @Bind(R.id.layout_address)
        View addressLayout;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface OnItemTouchListener {
        void onSelected(int position, ShippingAddressModel model);

        void onDelete(int position, ShippingAddressModel model);

        void onAddressSelected(int position, ShippingAddressModel model);
    }

    public void setOnItemTouchListener(OnItemTouchListener listener) {
        this.listener = listener;
    }
}
